package com.linwen.javase.desgin_pattern.structure.proxy.cjlib;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @ClassName CJLibProxy
 * @Description
 * @Author lin
 * @Date 2022/4/13 下午10:43
 * @Version V1.0
 */
public class CJLibProxy implements MethodInterceptor {

    private Object target;

    public Object getInstance(final Object target) {
        this.target = target;
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(this.target.getClass());
        enhancer.setCallback(this);
        return enhancer.create();
    }
    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("动态打出租广告");
        Object result = methodProxy.invokeSuper(o, objects);
        System.out.println("动态收中介费");
        return result;
    }
}
